--七日豪礼
local Msg = require("core.Msg")
local SevenDayGiftExcel = require("excel.present").sevenDayGift
local BuyExcel = require("excel.buy")
local BuyLogic = require("topup.BuyLogic")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local CommonDB = require("common.CommonDB")
local TopupLogic = require("topup.TopupLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local SceneHandler = require("scene.Handler")

SEVENDAYGIFT_BUY_NO = 0 --没买
SEVENDAYGIFT_BUY_HAD = 1 --已买

local function isBuy(human,id)
    local godDailyTopup = human.db.godDailyTopup
    local status = 0
    if godDailyTopup and godDailyTopup[id] and godDailyTopup[id].pay then
        status = 1
    end

    return status
end

-- 检查活动是否开启
function check(human,noSend)
	if not SceneHandler.canCharge(human) then
		return
	end

    local openLv = YunYingLogic.getOpenLvByPanelID(PanelDefine.PANEL_ID_62)
    if human.db.lv < openLv then
        if not noSend then
            local str = Util.format(Lang.ROLE_LEV_ERROR,openLv)
            return Broadcast.sendErr(human,str)
        end
    end

	for i=1,#SevenDayGiftExcel do
        local isBuy = isBuy(human,i)
        if isBuy == SEVENDAYGIFT_BUY_NO then
            return true
        end
    end
end

function makeGiftMsg(human,net,config,id)
    net.id = config.realId 
    local buyID = config.buyID
    net.giftVipExp = config.vipExp
   
    net.item[0] = #config.reward
    for j = 1,#config.reward do
        Grid.makeItem(net.item[j],config.reward[j][1],config.reward[j][2])
    end

    BuyLogic.fontBuyItem(human,net.buyMsg, buyID)

    local godDailyTopup = human.db.godDailyTopup
    local status = 0
    if godDailyTopup and godDailyTopup[id] and godDailyTopup[id].pay then
        status = 1
    end
    net.status = status
end

--七日豪礼查询
function query(human)
    if not check(human) and not SevenLoginDayLogic.isBaseOpen(human) then return end

    local msgRet = Msg.gc.GC_SEVENDAYGIFT_QUERY 
    local today = Util.diffDay(human.db.createTime) + 1
    if today > #SevenDayGiftExcel then
        today = #SevenDayGiftExcel + 1
    end
    msgRet.today = today
    msgRet.giftMsg[0] = #SevenDayGiftExcel
    for i=1,#SevenDayGiftExcel do
        makeGiftMsg(human,msgRet.giftMsg[i],SevenDayGiftExcel[i],i)
    end

    --Msg.trace(msgRet)
    Msg.send(msgRet,human.fd)
end

function dailyTopupBuy(human,id)
    if not check(human) and not SevenLoginDayLogic.isBaseOpen(human) then return end

    -- 礼包不存在
    local config = SevenDayGiftExcel[id]
    if not config then
        return Broadcast.sendErr(human,Lang.ACT_WAS_OVER)
    end

    local today = Util.diffDay(human.db.createTime) + 1
    if id > today then
        return Broadcast.sendErr(human,Lang.TOMORROW_OPEN)
    end

    -- 礼包已购买
    if human.db.godDailyTopup and human.db.godDailyTopup[id] and human.db.godDailyTopup[id].pay ~= nil then
        return Broadcast.sendErr(human,Lang.COMMON_LIBAO_HADBUY)
    end

    -- 写db
    human.db.godDailyTopup = human.db.godDailyTopup or {}
    human.db.godDailyTopup[id] = human.db.godDailyTopup[id] or {}
    human.db.godDailyTopup[id].pay = 1

    -- 发货
    BagLogic.addItemList(human, config.reward, "seven_DayGift")
    update(human,id)
	for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_102)
        break
    end
end

function update(human,id)
    local msgRet = Msg.gc.GC_SEVENDAYGIFT_UPDATE
    local config = SevenDayGiftExcel[id]
    if not config then return end

    makeGiftMsg(human,msgRet.giftMsg,config,id)

    --Msg.trace(msgRet)
    Msg.send(msgRet,human.fd)
end

function isBaseOpen(human,noSend)
	if not SceneHandler.canCharge(human) then
		return
	end

    if check(human,noSend) then
        return true
    end
end

function isOpen(human)
    if isBaseOpen(human,true) or SevenLoginDayLogic.isBaseOpen(human,true) then
        return true
    end
end